<?php
    namespace Home\Controller;
    use Home\Controller\HomeController;
//    use Home\Event\UserEvent;
    class UserController extends HomeController
    {
        //加载注册页面
        public function register ()
        {
            $this->title = "注册";
            $this->display("User:register");
        }

        //加载登录页面
        public function login ()
        {
            $this->title = "登录";
            $this->display("User:login");
        }

        //对注册信息进行判断
        Public function regDo ()
        {
            //创建事件控制器对象
            $userEvent = A("User","Event");
            if($userEvent -> regDo()){
                $userEvent ->updataIntegral("新用户赠送100积分",100);
                $this->success("注册成功,已自动登录,另外新用户赠送100积分,可抵扣1块钱",U("Index/index"));
            }else{
                $this->error("注册失败");
            }
        }

        //对登录信息进行判断
        Public function loginDo ()
        {
            //创建事件控制器对象
            $userEvent = A("User","Event");
            if($userEvent -> loginDo()){
                $this->success("登录成功",U("Index/index"));
            }

        }

        //退出登录
        Public function out ()
        {
            session("home",null);
            $this->success('退出成功',U('Index/index'));
        }

        //领取优惠券
        public function getCoupon ()
        {
            if(IS_GET){
                $this->isLogin();
                $coupon = A("User","Event");
                $row = $coupon -> getCoupon();

                if($row > 0){
                    $this->success("成功领取,继续购物吧");
                }else{
                    $this->error("领取失败");
                }
            }
        }

        //把商品添加到购物车
        public function addCart ()
        {
            if(IS_POST){

                $this->isLogin();
                if(I('post.act') == "add_cart"){
                    $cart = A("User","Event");
                    $insert_id = $cart ->addCart();
                    if($insert_id > 0){
                        $this->success("已添加进购物车");
                    }else{
                        $this->success("添加进购物车失败");
                    }
                }else{
                    //立即购买
                    $this->confirmOrder();
                }

            }
        }

        //加载购物车页面
        public function cart ()
        {
            $this->isLogin();
            $this->assign("title","我的购物车");
            //根据id查询该用户购物车里面的商品
            $cart = A("User","Event");
            $cartList = $cart -> showCart();
            $this->assign("cartList",$cartList);
            $this->display("User/cart");
        }

        //新建地址
        public function addAddress ()
        {
            if(IS_GET){
                $address = A("User","Event");
                $row = $address -> addAddress();
            }
        }

        //查询所有地址
        public function showAddress ()
        {
            if(IS_GET){
                $address = A("User","Event");
                $res= $address -> showAddress();
                echo json_encode($res);
            }
        }

        //设默认地址
        public function setDefault ()
        {
            if(IS_GET){
                $address = A("User","Event");
                $res= $address -> setDefault();
                echo $res;
            }
        }

        //购物车中的商品数量发生变动时,就直接把数量更新到购物车
        public function updateCart ()
        {
            if(IS_GET){
                $cart = A("User","Event");
                $cart->updateCart();
            }
        }

        //购物车中删除单个商品
        public function delCart ()
        {
            if(IS_GET){
                $cart = A("User","Event");
                $row = $cart -> delCart();
                if($row > 0){
                    $this->success("成功删除");
                }else{
                    $this->error("删除失败");
                }
            }
        }

        //清空购物车
        public function delAllCart ()
        {
            $cart = A("User","Event");
            $row = $cart -> delAllCart();
            if($row > 0){
                $this->success("已清空");
            }else{
                $this->error("清空失败");
            }
        }

        //确认订单页面
        public function  confirmOrder ()
        {
            if(IS_POST){
                $user = A("User","Event");
                $res = $user->confirmOrder();
                $this->assign("title","确认订单");
                $this->assign("orderList",$res);
//                dump($res);die;
                //得到应付的金额
                $sum = 0;
                foreach($res as $value){
                    $sum += $value['num']*$value['price'];
                }
                $this->assign("sum",$sum);
                //通过用户id查询他的所有优惠券
                $couponList = $user->queryAllCoupon();
                $this->assign("couponList",$couponList);
                //通过用户id查询他的所有积分
                $integralInfo = $user->queryIntegral();
                $this->assign("integralInfo",$integralInfo);
                //判断是从购物车页面购买还是直接购买
                $act = empty(I("post.act")) ? "buy" : I("post.act");
                $this->assign("act",$act);
                $uid =$_POST['uid'];
                $this->assign('uid',$uid);

                $this->display("User/order");
            }

        }

        //提交订单
        public function commitOrder ()
        {
//            $uid=$_POST['uid'];
//            dump($uid);die;
            if(IS_POST){
                $userEvent = A("User","Event");
                $res = $userEvent -> commitOrder();
                if($res == "失败的订单"){
                    $this->error("订单失败");
                }else{
                    $this->assign("title","付款");
                    $this->assign("uid",$uid);
                    $this->assign("payInfo",$res);
//                    dump($res);die;
                    $this->display("User/pay");
                }
            }
        }

        //付款
        public function pay()
        {
            $userEvent = A("User","Event");
            $row = $userEvent->pay();

        }

        //
    }







